package com.qst.rongxiaotong.controller;



import com.qst.rongxiaotong.common.Result;
import com.qst.rongxiaotong.context.BaseContext;
import com.qst.rongxiaotong.pojo.*;
import com.qst.rongxiaotong.service.BankService;
import com.qst.rongxiaotong.service.FinanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.qst.rongxiaotong.common.StatusCode;
import java.util.List;
@Api(tags = "融资模块接口")
@RestController
@RequestMapping("/finance")
@CrossOrigin
public class FinanceController {
    @Autowired
    private FinanceService financeService;
    @Autowired
    private BankService bankService;

    @ApiOperation("查询融资意向")
    @GetMapping("/selectIntentionByName")
    public Result selectIntentionByName(){
        String name = BaseContext.getCurrentuserName();
        List<Intention> intentionList=financeService.selectIntentionByName(name);
        return new Result<>(true, StatusCode.OK,"查询成功",intentionList);
    }

    @ApiOperation("添加融资意向")
    @PostMapping("/insertIntentionByName")
    public Result insertIntentionByName(@RequestBody Intention intention){
        String name = BaseContext.getCurrentuserName();
        intention.setUserName(name);
        financeService.insertIntentionByName(intention);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    //修改融资意向
    @ApiOperation("修改融资意向")
    @PutMapping("/updateIntentionByName")
    public Result updateIntentionByName(@RequestBody Intention intention){
        //获取登陆的用户名
        String name = BaseContext.getCurrentuserName();
        intention.setUserName(name);
        financeService.updateIntentionByName(intention);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    //删除融资意向
    @ApiOperation("删除融资意向")
    @DeleteMapping("/deleteIntentionByName")
    public Result deleteIntentionByName(){
        //获取登陆的用户名
        String name = BaseContext.getCurrentuserName();
        financeService.deleteIntentionByName(name);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    //查询推荐融资人
    @ApiOperation("查询推荐融资人")
    @GetMapping("/selectRecommendByName")
    public Result selectRecommendByName(){
        //获取登陆的用户名
        String name = BaseContext.getCurrentuserName();
        List<Recommender> list = financeService.selectRecommend(name);
        return new Result(true, StatusCode.OK, "查询成功",list);
    }


    //查询融资情况,显示我的融资信息
    @ApiOperation(value = "查询融资情况")
    @GetMapping("/selectByName")
    public Result selectByName() {
        String currentuserName = BaseContext.getCurrentuserName();
        Finance finance = new Finance();
        finance.setOwnName(currentuserName);

        List<Finance> finances = financeService.selectByFinance(finance);
        return new Result(true, StatusCode.OK, "查询成功", finances);
    }
    //根据id修改融资情报，用户修改个人融资信息，根据Financeid
    @ApiOperation(value = "根据id修改融资情报")
    @PutMapping("/update")
    public Result update(@RequestBody Finance finance) {
        System.out.println(finance);
        financeService.updateByFinance(finance);
        return new Result(true, StatusCode.OK, "修改成功");
    }
    //根据id删除融资情报，用户删除个人融资信息，根据Financeid
    @ApiOperation(value = "根据id删除融资情报")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        financeService.deleteByFinanceId(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }
    //根据Financeid查询融资情报,信息回显
    @ApiOperation(value = "根据id查询融资情报")
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable("id") Integer id) {
        Finance finance = financeService.selectByFinanceId(id);

        return new Result(true, StatusCode.OK, "查询成功", finance);
    }


//=================================================================================融资申请
    //查询银行情报(融资申请)
    @ApiOperation(value = "查询银行情报")
    @GetMapping("/selectbank")
    public Result selectbank() {

        List<Bank> banks = bankService.selectAllBank();
        return new Result(true, StatusCode.OK, "查询成功", banks);

    }
    //添加单人贷款
    @ApiOperation(value = "添加单人贷款")
    @PostMapping("/add")
    public Result add(@RequestBody Finance finance) {
        String currentuserName = BaseContext.getCurrentuserName();
        finance.setOwnName(currentuserName);
        financeService.add(finance);
        return new Result(true, StatusCode.OK, "申请成功");
    }
    //添加组合贷款
    @ApiOperation(value = "添加组合贷款")
    @PostMapping("/addFinanceMulti")
    public Result addFinanceMulti(@RequestBody Finance finance) {
        //获取登陆的用户名
        String currentuserName = BaseContext.getCurrentuserName();
        finance.setOwnName(currentuserName);
        financeService.addMulti(finance);
        return new Result(true, StatusCode.OK, "申请成功");
    }
    //单人贷款
    @ApiOperation(value = "单人贷款")
    @GetMapping("/selectFinaceUser/{bankId}")
    public Result selectFinaceUser(@PathVariable("bankId") String bankId){
        //获取登陆的用户名
        String currentuserName = BaseContext.getCurrentuserName();
        System.out.println("bankId:"+bankId);
        FinaceUserDetails finaceUserDetails = financeService.selectFinaceUser(currentuserName,bankId);

        return new Result(true, StatusCode.OK, null,finaceUserDetails);
    }













}
